<?php namespace Home\Controller; 
use Hdphp\Cart\Cart;
/**
 * 列表控制器
 */
class ContController extends Cart {
	public function __construct()
    {
        
    }
	public function index()
	{	$gid=Q('get.gid',0,'intval');
		$tid=Q('get.tid',0,'intval');
		if(IS_AJAX){
			if(empty($_POST['gaid'][1])) return false;
			$data1=array( 
			    'id'        => $_POST['gid'], // 商品 ID 
			    'name'      =>$_POST['gname'],// 商品名称 
			    'num'       => $_POST['stocks'], // 商品数量 
			    'price'     => $_POST['sale'], // 商品价格 
			    'options' => $_POST['gaid'],// 其他参数如价格、颜色、可以为数组或字符串 
			    'color'     =>$_POST['gaid'][1], 
			    'size'  => $_POST['gaid'][0] 
			);
			Cart::add($data1); // 添加到购物车
		}
		$num1=cart::getTotalNums($_SESSION);
		$datat=Db::table('Category')->where("pid=0")->orderBy('csort','DESC')->limit(8)->get();
		$data=Db::table('goods')->join('goods_cont','gid','=','goods_cont.goods_gid')->where("gid={$gid}")->first();
		$data['photo']=explode("|",$data['photo']);
		$spec=Db::table('type_att')->where("type_tid={$tid} and tatype=1")->field('taid,taname')->get();
		foreach ($spec as $k => $v)
		{
			$spec[$k]['opt']=Db::table('goods_att')->field('gaid', 'gavalue','goods_gid')->where("goods_gid={$gid} and type_att_taid={$v['taid']}")->get();
		}
		$specl=Db::table('type_att')->where("type_tid={$tid} and tatype=0")->field('taid,taname')->get();
		foreach ($specl as $k => $v)
		{
			$specl[$k]['opt']=Db::table('goods_att')->field('gaid', 'gavalue')->where("goods_gid={$gid} and type_att_taid={$v['taid']}")->first();
		}
		View::with('datat',$datat);
		View::with('data',$data);
		View::with('spec',$spec);
		View::with('specl',$specl);
		View::with('num1',$num1);
		View::make();
	}
	public function ajaxGetAttr()
	{
		if(IS_AJAX){
			$data=Q('post.data');
			$set=Q('post.set');
				foreach ($data['gaid'] as $k => $v)
				{
					$ajdata[$k]=Db::table('goods_att')->field('gaid', 'gavalue')->where("gaid={$v}")->first();
				}
				echo json_encode($ajdata);
				exit;	
		}
	}

}

?>